<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>线程安全：多线程编程的核心挑战</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 8px 16px;
            font-size: 14px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .concept-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            border-left: 4px solid #667eea;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-24 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-7xl font-bold mb-6 serif-font">线程安全</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">Thread Safety</p>
            <div class="max-w-3xl mx-auto">
                <p class="text-lg leading-relaxed opacity-85">
                    在多线程的世界里，保持程序的正确性和一致性是一项充满挑战的艺术。
                    线程安全不仅是技术问题，更是设计哲学的体现。
                </p>
            </div>
            <div class="mt-12 flex justify-center space-x-8">
                <div class="text-center">
                    <i class="fas fa-shield-alt text-4xl mb-2"></i>
                    <p class="text-sm">数据保护</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-sync-alt text-4xl mb-2"></i>
                    <p class="text-sm">同步机制</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-lock text-4xl mb-2"></i>
                    <p class="text-sm">并发控制</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap serif-font">线</span>程安全（Thread Safety）是指在多线程环境下，多个线程并发访问同一资源时，程序仍然能够保持正确性和一致性，而不引发数据竞态条件、数据损坏或不一致的状态。实现线程安全的关键在于正确地管理对共享资源的访问，确保线程之间的操作不会互相干扰。
                </p>
            </div>
        </section>

        <!-- Core Concepts -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 text-gray-800 serif-font">
                <i class="fas fa-brain text-purple-600 mr-3"></i>核心概念
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6 mb-12">
                <div class="concept-card p-6 rounded-xl card-hover">
                    <h3 class="text-xl font-bold mb-3 text-gray-800">
                        <i class="fas fa-exclamation-triangle text-yellow-500 mr-2"></i>数据竞态条件
                    </h3>
                    <p class="text-gray-600">当两个或多个线程并发访问共享数据，并且至少有一个线程修改数据时，程序的执行结果依赖于线程执行的顺序。</p>
                </div>
                
                <div class="concept-card p-6 rounded-xl card-hover">
                    <h3 class="text-xl font-bold mb-3 text-gray-800">
                        <i class="fas fa-eye text-blue-500 mr-2"></i>内存可见性
                    </h3>
                    <p class="text-gray-600">确保一个线程对共享变量的修改对其他线程是可见的。通过使用 volatile 关键字或同步机制来保证。</p>
                </div>
                
                <div class="concept-card p-6 rounded-xl card-hover">
                    <h3 class="text-xl font-bold mb-3 text-gray-800">
                        <i class="fas fa-atom text-green-500 mr-2"></i>原子性
                    </h3>
                    <p class="text-gray-600">确保操作不可分割，即一个操作要么完全执行，要么完全不执行。通过同步机制、原子变量或事务来实现。</p>
                </div>
                
                <div class="concept-card p-6 rounded-xl card-hover">
                    <h3 class="text-xl font-bold mb-3 text-gray-800">
                        <i class="fas fa-redo text-purple-500 mr-2"></i>可重入性
                    </h3>
                    <p class="text-gray-600">如果一个线程已经获得了某个锁，则它可以再次获得该锁而不会被阻塞。ReentrantLock 和 synchronized 都是可重入锁。</p>
                </div>
            </div>

            <!-- Synchronization Mechanisms -->
            <div class="bg-gray-100 rounded-2xl p-8 mb-12">
                <h3 class="text-2xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-cogs text-indigo-600 mr-2"></i>同步机制
                </h3>
                <div class="grid md:grid-cols-2 lg:grid-cols-4 gap-4">
                    <div class="bg-white p-4 rounded-lg text-center">
                        <i class="fas fa-lock text-3xl text-red-500 mb-2"></i>
                        <h4 class="font-bold mb-1">锁</h4>
                        <p class="text-sm text-gray-600">synchronized, ReentrantLock</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg text-center">
                        <i class="fas fa-traffic-light text-3xl text-yellow-500 mb-2"></i>
                        <h4 class="font-bold mb-1">信号量</h4>
                        <p class="text-sm text-gray-600">Semaphore</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg text-center">
                        <i class="fas fa-bell text-3xl text-blue-500 mb-2"></i>
                        <h4 class="font-bold mb-1">条件变量</h4>
                        <p class="text-sm text-gray-600">Condition, wait/notify</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg text-center">
                        <i class="fas fa-bolt text-3xl text-green-500 mb-2"></i>
                        <h4 class="font-bold mb-1">原子操作</h4>
                        <p class="text-sm text-gray-600">AtomicInteger, AtomicReference</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Implementation Methods -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 text-gray-800 serif-font">
                <i class="fas fa-tools text-purple-600 mr-3"></i>实现方式
            </h2>

            <!-- Mermaid Diagram -->
            <div class="bg-white rounded-2xl shadow-lg p-8 mb-8">
                <div class="mermaid">
                    graph TD
                        A[线程安全实现] --> B[不可变对象]
                        A --> C[局部变量]
                        A --> D[同步容器]
                        A --> E[锁机制]
                        A --> F[原子变量]
                        A --> G[线程局部变量]
                        
                        B --> B1[String]
                        B --> B2[Integer]
                        
                        D --> D1[ConcurrentHashMap]
                        D --> D2[CopyOnWriteArrayList]
                        
                        E --> E1[显式锁 ReentrantLock]
                        E --> E2[隐式锁 synchronized]
                        
                        F --> F1[AtomicInteger]
                        F --> F2[AtomicLong]
                        
                        G --> G1[ThreadLocal]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                        style B fill:#f59e0b,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#10b981,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#3b82f6,stroke:#fff,stroke-width:2px,color:#fff
                        style E fill:#ef4444,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#8b5cf6,stroke:#fff,stroke-width:2px,color:#fff
                        style G fill:#ec4899,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>

            <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-cube text-3xl text-orange-500 mr-3"></i>
                        <h3 class="text-xl font-bold">不可变对象</h3>
                    </div>
                    <p class="text-gray-600">不可变对象在创建之后其状态无法改变，因此在多线程环境下天然是线程安全的。</p>
                </div>

                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-map-marker-alt text-3xl text-green-500 mr-3"></i>
                        <h3 class="text-xl font-bold">局部变量</h3>
                    </div>
                    <p class="text-gray-600">局部变量在线程内部创建，每个线程都有自己的副本，不会有线程安全问题。</p>
                </div>

                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-box text-3xl text-blue-500 mr-3"></i>
                        <h3 class="text-xl font-bold">同步容器</h3>
                    </div>
                    <p class="text-gray-600">使用 Collections.synchronizedXXX() 或并发集合类如 ConcurrentHashMap。</p>
                </div>

                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-key text-3xl text-red-500 mr-3"></i>
                        <h3 class="text-xl font-bold">锁机制</h3>
                    </div>
                    <p class="text-gray-600">使用显式锁（ReentrantLock）或隐式锁（synchronized）进行同步控制。</p>
                </div>

                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-atom text-3xl text-purple-500 mr-3"></i>
                        <h3 class="text-xl font-bold">原子变量</h3>
                    </div>
                    <p class="text-gray-600">使用 java.util.concurrent.atomic 包中的原子类实现线程安全操作。</p>
                </div>

                